package org.ldh.web.servlet;

import org.ldh.domain.Book;
import org.ldh.service.BookService;
import org.ldh.service.impl.BookServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

@WebServlet("/bookServlet.action")
public class BookServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        System.out.println("--------bookServlet.action--------");
        //设置编码
        request.setCharacterEncoding("utf-8");
        //获取请求参数
        String action = request.getParameter("action");
        System.out.println("action: "+action);
        //调用service层
        BookService bookService = new BookServiceImpl();

        //判断请求参数
        if(action != null && action.equals("list")){
            //查询
            List<Book> books = new ArrayList<Book>();
            //查询数据库
            books = bookService.findAll();
            //将查询到的图书信息储存到request域中
            request.setAttribute("books",books);
            //跳转到列表页
            request.setAttribute("mainRight","/WEB-INF/pages/bookList.jsp");
            request.getRequestDispatcher("/WEB-INF/pages/main.jsp").forward(request,response);
        }else if(action != null && action.equals("add")){
            //添加
            //跳转到添加页面
            request.setAttribute("mainRight","/WEB-INF/pages/bookAdd.jsp");
            request.getRequestDispatcher("/WEB-INF/pages/main.jsp").forward(request,response);
        }else if(action != null && action.equals("save")){
            //保存
            //获取请求参数
            String bookName = request.getParameter("bookName");
            String bookType = request.getParameter("bookType");
            String bookPress = request.getParameter("bookPress");
            String bookPrice = request.getParameter("bookPrice");
            Book book = bookService.findByName(bookName);
            System.out.println(book);
            if(book == null) {
                //说数据库中不存在该图书

                //将数据封装到实体类
                Book book2 = new Book();
                book2.setBookName(bookName);
                book2.setBookType(bookType);
                book2.setBookPress(bookPress);
                book2.setBookPrice(Double.parseDouble(bookPrice));
                //调用service层的保存方法
                bookService.saveBook(book2);
                //保存完成后，重新查询数据库
                List<Book> books = bookService.findAll();
                //重新存储到request域
                request.setAttribute("books", books);
                //跳转到列表页，显示图书信息列表
                //跳转到列表页
                request.setAttribute("mainRight", "/WEB-INF/pages/bookList.jsp");
                request.getRequestDispatcher("/WEB-INF/pages/main.jsp").forward(request, response);
            }else{
                //说明数据库中已经存在该图书
                //存储错误信息到request域
                request.setAttribute("error","该图书已经存在！请重新输入");
                //跳转到添加页面
                request.setAttribute("mainRight","/WEB-INF/pages/bookAdd.jsp");
                request.getRequestDispatcher("/WEB-INF/pages/main.jsp").forward(request,response);
            }
        }else if(action != null && action.equals("update")){
            //获取请求参数
            String bookId = request.getParameter("bookId");
            //根据id查询
            Book book = bookService.findById(bookId);
            //获取图书数据
            String bookName = book.getBookName();
            String bookType = book.getBookType();
            String bookPress = book.getBookPress();
            double bookPrice = book.getBookPrice();
            //存储到request域
            request.setAttribute("bookId",bookId);
            request.setAttribute("bookName",bookName);
            request.setAttribute("bookType",bookType);
            request.setAttribute("bookPress",bookPress);
            request.setAttribute("bookPrice",bookPrice);
            //跳转修改页面
            request.setAttribute("mainRight","/WEB-INF/pages/bookUpdate.jsp");
            request.getRequestDispatcher("/WEB-INF/pages/main.jsp").forward(request,response);
        }else if(action != null && action.equals("reUpdate")) {
            //更新
            //获取请求参数
            String bookId = request.getParameter("bookId");
            String bookName = request.getParameter("bookName");
            String bookType = request.getParameter("bookType");
            String bookPress = request.getParameter("bookPress");
            String bookPrice = request.getParameter("bookPrice");
            //封装实体类
            Book book = new Book();
            book.setBookId(Integer.parseInt(bookId));
            book.setBookName(bookName);
            book.setBookType(bookType);
            book.setBookPress(bookPress);
            book.setBookPrice(Double.parseDouble(bookPrice));
            //调用service层的修改方法
            bookService.update(book);
            //修改完成之后，重新查询数据库
            List<Book> books = bookService.findAll();
            //重新存储到request域
            request.setAttribute("books", books);
            //跳转到列表页，显示图书信息列表
            request.setAttribute("mainRight", "/WEB-INF/pages/bookList.jsp");
            request.getRequestDispatcher("/WEB-INF/pages/main.jsp").forward(request, response);
        }else if(action != null && action.equals("delete")){
            //获取请求参数
            String bookId = request.getParameter("bookId");
            //根据id删除图书
            bookService.deleteById(bookId);
            //删除完成之后，重新查询数据库
            List<Book> books = bookService.findAll();
            //重新存储到request域
            request.setAttribute("books", books);
            //跳转到列表页，显示图书信息列表
            request.setAttribute("mainRight", "/WEB-INF/pages/bookList.jsp");
            request.getRequestDispatcher("/WEB-INF/pages/main.jsp").forward(request, response);
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}
